package net.xdclass.service.service.impl;

import net.xdclass.service.domain.Product;
import net.xdclass.service.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * author: 何建文
 * create: 2020-07-18 15:48
 * description:
 **/
@Service
public class ProductServiceImpl implements ProductService {

    private final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    private static Map<Integer, Product> daoMap = new HashMap<>();

    static {
        Product product1 = new Product(1, "iphonex", 9999, 10);
        Product product2 = new Product(2, "冰箱", 212, 454);
        Product product3 = new Product(3, "洗衣机", 5434, 356);
        Product product4 = new Product(4, "电话", 2467, 125);
        Product product5 = new Product(5, "汽车", 3575, 456);
        Product product6 = new Product(6, "椅子", 8567, 896);
        Product product7 = new Product(7, "编程思想", 7685, 456);
        daoMap.put(product1.getId(), product1);
        daoMap.put(product2.getId(), product2);
        daoMap.put(product3.getId(), product3);
        daoMap.put(product4.getId(), product4);
        daoMap.put(product5.getId(), product5);
        daoMap.put(product6.getId(), product6);
        daoMap.put(product7.getId(), product7);
    }

    @Override
    public List<Product> listProduct() {
        return new ArrayList<>(daoMap.values());
    }

    @Override
    public Product findById(int id) {
        /**
         * 临时测试代码，先注释
         */
        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("service findById");
        return daoMap.get(id);
    }
}
